package inf.coach.mobile;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity  {
	public static final String SETTING_INFOS = "SETTING_Infos"; 
	public static final String NAME = "NAME"; 
	public static final String PASSWORD = "PASSWORD"; 
	private EditText username;
	private EditText password;
	private Button login;
	private Button register;
	private Button gpstest;
	private Button orientation;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//      setTitle("login page,welcome to coach system");
        setTitle("Login Seite, Willkommen bei Coach System");
		username = (EditText)findViewById(R.id.editText_Username) ;
		password = (EditText) findViewById(R.id.editText_Password);
		login = (Button) findViewById(R.id.button_Login);
//		register = (Button) findViewById(R.id.button_register);
//		gpstest = (Button)findViewById(R.id.gps);
//		orientation=(Button)findViewById(R.id.button1);
		
//		register.setVisibility(View.INVISIBLE);
//		gpstest.setVisibility(View.INVISIBLE);
//		orientation.setVisibility(View.INVISIBLE);
		  /** Automatic login. */
		SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0); //获取一个SharedPreferences对象 
		String Name = settings.getString(NAME, "");  //取出保存的NAME 
		String Password = settings.getString(PASSWORD, ""); //取出保存的PASSWORD 

//		Set value 
		username.setText(Name);  //将取出来的用户名赋予username
		password.setText(Password);  //将取出来的密码赋予password
		
		/** login button action. */  
		
		login.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				String un = username.getText().toString();
				String pw = password.getText().toString();
				if (un.equals("") || pw.equals("")) {
					message.show(Login.this, "Fehler",
							"Bitte  Benutzername und Kennwort eingeben !");

				} 
				else {

					if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
					{
										
						String text = webservice.login(un, pw);
						if (!text.equals("null") && !text.equals("0")) {
						
						
						//message.show(Login.this,"ok","Successful!"+text);
						
						Intent intent = new Intent();

						intent.setClass(Login.this, MainMenu.class);
						Bundle bundle = new Bundle();

						bundle.putLong("userid", Integer.parseInt(text));
						/* 将Bundle 对象assign 给Intent */
						intent.putExtras(bundle);
						/* 调用Activity EX03_10_1 */
						startActivity(intent);

					}
					if (text.equals("0")) {
						
						message.show(Login.this, "Fehler",
								"Falscher Benutzername oder falsches Kennwort !");


					}
					if(text.equals("null"))
					{
						message.show(Login.this,"Fheler","Service nicht gefunden, bitte überprüfen Internetverbindung!");
					}
				}
					else{
					message.show(Login.this,"Fehler", "Keine Internetverbindung !");
				}
					}
			}
		});
		
/*		register.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
				
				message.show(Login.this,"Information", "please register on the web site.");
				
				} 
		});
		//gpstest.setEnabled(false);
		gpstest.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
				
				Intent intent = new Intent();

				intent.setClass(Login.this, GpsTest.class);
				
				startActivity(intent);
				
				} 
		});
		
		orientation.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
				
				Intent intent = new Intent();

				intent.setClass(Login.this, Orientation.class);
				
				startActivity(intent);
				
				} 
		});*/

    }
    
	protected void onRestart() {
		
		super.onRestart();
/*		if(!username.getText().toString().equals("")&&!password.getText().toString().equals(""))
		{
			login.callOnClick();
		}
		*/
	}
    @Override 

    protected void onStop(){ 

        super.onStop(); 

SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0); //首先获取一个SharedPreferences对象 

settings.edit() 

.putString(NAME, username.getText().toString()) 

.putString(PASSWORD, password.getText().toString()) 

.commit(); 

    } //将用户名和密码保存进去 


    
    protected void dialog() { 
        AlertDialog.Builder builder = new Builder(Login.this); 
        builder.setMessage("Möchten Sie das Programm beenden ?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Login.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    
    /** override option key action. */ 
 
    
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialog(); 
            return false; 
        }
        if(keyCode== KeyEvent.KEYCODE_SEARCH&& event.getRepeatCount() == 0){
        	return true;
        }
        return false; 
    }
    
   

    
}